package com.inossem.warehousestorage_inossem.activity.system;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.base.PingRequest;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.library.smoothCheckBox.SmoothCheckBox;

public class SettingAddressActivity extends BaseActivity {
    private TextView title;

    private EditText domain, port;

    private Button save;

    private SmoothCheckBox isFocus;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting_address);
        initView(SettingAddressActivity.this);
        initData(SettingAddressActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        domain = (EditText) activity.findViewById(R.id.domain);
        port = (EditText) activity.findViewById(R.id.port);
        save = (Button) activity.findViewById(R.id.save);
//        isFocus = (SmoothCheckBox) activity.findViewById(R.id.isFocus);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_setting_address_access_Address_settings));
        domain.setText(SPUtils.getDomain(activity));
        port.setText(SPUtils.getPort(activity));
        save.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String result = check(activity);
                if (result == null) {
                    String domainStr = domain.getText().toString().trim();
                    String portStr = port.getText().toString().trim();
                    PingRequest.request(activity, domainStr, portStr, new GsonBeanListener() {
                        @Override
                        public void onSuccess(Object bean) {
                            SPUtils.saveDomain(activity, domain.getText().toString().trim());
                            SPUtils.savePort(activity, port.getText().toString().trim());
                            Utils.middleToast(activity, getResources().getString(R.string.activity_setting_address_set_up_the_success) + domainStr + "\n" + getResources().getString(R.string.activity_setting_address_sport)+ portStr);
                            finish();
                        }

                        @Override
                        public void onError(Context context, String code, String msg) {
                            Utils.middleToast(context, getResources().getString(R.string.activity_setting_address_domain_name_and_port_settings_failed));
                        }

                        @Override
                        public boolean useDefaultError() {
                            return false;
                        }
                    });
                } else {
                    Utils.middleToast(activity, result);
                }
            }
        });

//        isFocus.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
//                if (isChecked) {
//                    LogUtils.i("yes");
//                } else {
//                    LogUtils.i("no");
//                }
//            }
//        });
    }

    private String check(Context context) {
        if (TextUtils.isEmpty(domain.getText().toString())) {
            return getResources().getString(R.string.activity_setting_address_domain_name_should_not_be_empty);
        }
        if (TextUtils.isEmpty(port.getText().toString())) {
            return getResources().getString(R.string.activity_setting_address_the_port_must_not_be_empty);
        }
        return null;
    }

    @Override
    public void onBackPressed() {
    }

}
